package com.qimo.controller;


import com.qimo.model.IndexShoppingListItem;
import com.qimo.model.Item;
import com.qimo.service.ItemService;
import com.qimo.tool.ResultInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class ItemController {
    @Resource
    private ItemService ItemService;

    @ResponseBody
    @RequestMapping("/index/itemList")
    public ResultInfo indexItemList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入itemLIst");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String shopName,typeName,ISBN,itemName;
        ISBN=request.getParameter("ISBN");
        typeName=request.getParameter("typeName");
        shopName=request.getParameter("shopName");
        itemName=request.getParameter("itemName");
        int page=Integer.parseInt(request.getParameter("page"));//无需-1
        System.out.println(ISBN);
        System.out.println(typeName);
        System.out.println(shopName);
        System.out.println(itemName);
        System.out.println(page);

        List<IndexShoppingListItem> list=this.ItemService.IndexItemList(ISBN,typeName,shopName,itemName,page);
        int count= this.ItemService.IndexItemListCount(ISBN,typeName,shopName,itemName);
        if(list.isEmpty())//无结果
        {
            System.out.println("list:null");
            ResultInfo resultInfo=new ResultInfo();
            resultInfo.setCode(0);//失败了
            resultInfo.setCount(count);
            resultInfo.setMsg("无结果");
            return resultInfo;
        }
        else {

            System.out.println(list);
            ResultInfo resultInfo=new ResultInfo();
            resultInfo.setCode(1);
            resultInfo.setCount(count);
            resultInfo.setMsg("查询成功");
            resultInfo.setResult(list);
            return resultInfo;

        }
    }

    @ResponseBody
    @RequestMapping("/item/itemList")
    public ResultInfo itemItemList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入itemitem");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int itemId=Integer.parseInt(request.getParameter("itemId"));
        System.out.println(itemId);
        Item item =this.ItemService.ItemItem(itemId);
        if(item==null)//无结果
        {
            System.out.println("itemController出现错误，讲道理前台是不会允许传来空值的");
            return ResultInfo.failure("讲道理前台是不会允许传来空值的");
        }
        else {
            System.out.println(item);
            ResultInfo resultInfo=new ResultInfo();
            resultInfo.setCode(1);
            resultInfo.setMsg("查询成功");
            resultInfo.setResult(item);
            return resultInfo;

        }
    }
}
